<?
class UrlManager {
	var $slug = "";
	var $page = '';
	var $filename = '';
	var $params = array();
	var $controller = '';
	var $action = '';
	var $redirect = array(
						array("/descargar","/descargar.php")
					);
	var $layout = 'index.php';
	function __construct(){
		$this->slug = $_GET['slug'];
		if($this->slug) $this->parseSlug($this->slug);
	}
	function setSlug($nslug){
		$this->parseSlug($this->slug = $nslug);
	}
	private function parseSlug($slug){
// 		if($this->fastSlug($slug)) return true;
		$a = explode('?',$slug);
		$b = explode('/',$a[0]);
		if($b[0]) $this->controller = array_shift($b);
		if($b[0]) $this->action = array_shift($b);
		else $this->page = $this->controller;
		/*if(sizeof($b))*/ $this->params = $b;
// 		$this->address = "/".$this->controller;
// 		if($this->action) $this->address .= "/".$this->action;
	}
	function fastSlug($slug){
		$return = false;
		if($filename = $this->_get_filename($slug)){
			$this->filename = $filename;
			$return = true;
		}
		return $return;
	}
	private function _get_filename($slug){
		$filename = false;
		$param_file = $this->params[sizeof($this->params)-1];
		if(file_exists(ROOT.$slug) && !is_dir(ROOT.$slug)){
			$filename = $slug;
// 		}else if(file_exists(ROOT.''.$slug.'.php')){
// 			$filename = ROOT.''.$slug.'.php';
// 		}else if(file_exists(PHP_DIR.''.$slug)){
// 			$filename = PHP_DIR.''.$slug;
		}else if(file_exists(ROOT.'views/pages/'.$slug.'.php')){
			$filename = ROOT.'views/pages/'.$slug.'.php';
		}elseif($param_file && file_exists(ROOT.$param_file) && !is_dir(ROOT.$param_file)){
			$filename = $param_file;
		}
// 		var_dump($filename);
		return $filename;
	}
	function render($opts = array()){
//         global $xajax, $AU, $AL, $SESSION, $APerM, $ITEM, $CATEGORIA, $FAIL, $CONFIG_ARRAYS, $FICHA, $titulo_permalink, $TITULO, $CATEGORIA_TXT;
		if(!$this->filename){
			if($this->controller && $this->action && (class_exists($this->controller,false) || file_exists(CLASS_DIR.$this->controller.".php"))){
				$controller = new $this->controller;
			}
			if($controller && method_exists($controller,$this->action)){
				$controller->UrlManager = $this;
				$controller->{$this->action}($this->params[0],$this->params[1],$this->params[2]);
			}
			if(file_exists(LAYOUTS_DIR.$this->layout)){
				$this->filename = LAYOUTS_DIR.$this->layout;
			}elseif($filename = $this->_get_filename($this->page)){
				$this->filename = $filename;
			}else/*if($this->page)*/{
				switch($this->page){
					default:
						$this->filename = 'views/layouts/index.php';
				}
			}
		}

		$id_client = $this->id_client;
		if(strpos($this->filename,ROOT) === false) $this->filename = ROOT.$this->filename;
// 		var_dump($this->filename);
		include($this->filename);
	}
	function setPage($np){
		$this->controller = $this->page = $np;
	}
	function getSlug(){
		return $this->slug;
	}
	function setAction($na){
		return $this->action = $na;
	}
	function setController($na){
		return $this->controller = $na;
	}
	function getPage(){
		return $this->page;
	}
	function getLastParam(){
		$size = sizeof($this->params);
// 		var_dump($this->page);
		if($size) return $this->params[$size-1];
		else if($this->page) return $this->page;
		else $this->slug;
	}
	function getController(){
		return $this->controller;
	}
	function getAction(){
		return $this->action;
	}
	function blank($cnt){
		$this->layout='blank.php';
		$this->content = $cnt;
	}
}